﻿Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel

Public Class lbox
    Inherits ListBox

    Public Sub New()
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.OptimizedDoubleBuffer, True)
        Me.ItemHeight = 40
        Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
        Me.BackColor = SystemColors.Control

    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim g As Graphics = e.Graphics

        g.SmoothingMode = SmoothingMode.AntiAlias
        g.InterpolationMode = InterpolationMode.HighQualityBicubic

        g.Clear(BackColor)

        Dim rct_background As Rectangle = Me.ClientRectangle

        For i As Integer = Me.Items.Count - 1 To 0 Step -1
            If Me.SelectedIndex <> i Then
                Dim rct As Rectangle = Me.GetItemRectangle(i)

                'Dim lng As New LinearGradientBrush(rct, SystemColors.ControlLightLight, SystemColors.ControlLight, 90.0F)

                'g.FillRectangle(lng, rct)

                Try
                    Dim p() As String = Split(Me.Items(i), " | ")

                    g.DrawString(p(0), New Font("Segoe UI", 14), Brushes.Black, rct, New StringFormat With {.Alignment = StringAlignment.Near, .LineAlignment = StringAlignment.Center})

                    g.DrawString(p(1), New Font("Segoe UI", 9), Brushes.DimGray, rct, New StringFormat With {.Alignment = StringAlignment.Far, .LineAlignment = StringAlignment.Near})

                    g.DrawString(p(2), New Font("Segoe UI", 9, FontStyle.Italic), Brushes.DimGray, rct, New StringFormat With {.Alignment = StringAlignment.Far, .LineAlignment = StringAlignment.Far})

                Catch ex As Exception

                End Try

                Dim p1 As New Point(rct.X, rct.Y + rct.Height)
                Dim p2 As New Point(rct.X + rct.Width, rct.Y + rct.Height)

                g.DrawLine(Pens.Silver, p1, p2)
            End If
        Next

        If Me.SelectedIndex <> -1 Then
            Dim rct As Rectangle = Me.GetItemRectangle(Me.SelectedIndex)

            'Dim lng As New LinearGradientBrush(rct, SystemColors.ControlLight, SystemColors.ControlLightLight, 90.0F)

            g.FillRectangle(Brushes.Black, rct)

            Try
                Dim p() As String = Split(Me.Items(Me.SelectedIndex), " | ")

                g.DrawString(p(0), New Font("Segoe UI", 14), Brushes.White, rct, New StringFormat With {.Alignment = StringAlignment.Near, .LineAlignment = StringAlignment.Center})

                g.DrawString(p(1), New Font("Segoe UI", 9), Brushes.White, rct, New StringFormat With {.Alignment = StringAlignment.Far, .LineAlignment = StringAlignment.Near})

                g.DrawString(p(2), New Font("Segoe UI", 9, FontStyle.Italic), Brushes.White, rct, New StringFormat With {.Alignment = StringAlignment.Far, .LineAlignment = StringAlignment.Far})

            Catch ex As Exception

            End Try

            Dim p1 As New Point(rct.X, rct.Y + rct.Height)
            Dim p2 As New Point(rct.X + rct.Width, rct.Y + rct.Height)

            g.DrawRectangle(Pens.DimGray, rct)
        End If

        rct_background.Width -= 1
        rct_background.Height -= 1

        'g.DrawRectangle(New Pen(Brushes.DimGray, 1), rct_background)
    End Sub

    Protected Overrides Sub OnSelectedIndexChanged(e As EventArgs)
        MyBase.OnSelectedIndexChanged(e)
        Me.Invalidate()
    End Sub
End Class
